<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class LandingCompany extends Model
{
    protected $table = 'landing_companies';

    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';

    // 账户类型常量
    protected $fillable = [
        'name',
        'short_name',
        'code',
        'tax_number',
        'address',
        'contact_person',
        'phone',
        'email',
        'status',
        'billing_categories',
        'remark',
        'bank_name',
        'account_number',
        'balance',
    ];

    protected $casts = [
        'balance' => 'decimal:2',
        'billing_categories' => 'array',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function invoices(): HasMany
    {
        return $this->hasMany(Invoice::class);
    }

    public function transactions(): HasMany
    {
        return $this->hasMany(Transaction::class);
    }

    public function scopeActive($query)
    {
        return $query->where('status', self::STATUS_ACTIVE);
    }

    public function scopeInactive($query)
    {
        return $query->where('status', self::STATUS_INACTIVE);
    }

    public function scopeSearchByName($query, $name)
    {
        return $query->where('name', 'like', "%{$name}%")
                    ->orWhere('short_name', 'like', "%{$name}%")
                    ->orWhere('code', 'like', "%{$name}%");
    }

    public function getStatusDisplayAttribute(): string
    {
        $statusMap = [
            self::STATUS_ACTIVE => '启用',
            self::STATUS_INACTIVE => '停用'
        ];

        return $statusMap[$this->status] ?? '未知';
    }

    public static function getStatusOptions(): array
    {
        return [
            self::STATUS_ACTIVE => '启用',
            self::STATUS_INACTIVE => '停用'
        ];
    }


    // 更新余额
    public function updateBalance($amount, $type = 'add'): bool
    {
        if ($type === 'add') {
            $this->balance = ($this->balance ?? 0) + $amount;
        } else {
            $this->balance = ($this->balance ?? 0) - $amount;
        }

        return $this->save();
    }

    // 检查余额是否充足
    public function hasEnoughBalance($amount): bool
    {
        return ($this->balance ?? 0) >= $amount;
    }

    // 获取激活的落地公司选项
    public static function getActiveCompanies(): array
    {
        return self::where('status', self::STATUS_ACTIVE)
            ->select(['id', 'name', 'short_name', 'code', 'billing_categories'])
            ->get()
            ->map(function ($company) {
                return [
                    'value' => $company->id,
                    'label' => $company->name,
                    'name' => $company->name,
                    'code' => $company->code,
                    'billing_categories' => $company->billing_categories ?: []
                ];
            })
            ->toArray();
    }

    // 获取所有公司的开票类目
    public static function getAllBillingCategories(): array
    {
        return self::where('status', self::STATUS_ACTIVE)
            ->whereNotNull('billing_categories')
            ->select(['id', 'name', 'billing_categories'])
            ->get()
            ->map(function ($company) {
                return [
                    'company_id' => $company->id,
                    'company_name' => $company->name,
                    'categories' => $company->billing_categories ?: []
                ];
            })
            ->toArray();
    }
}
